package com.dyh.quickdemo.demos.rxjava;

/*
@Author=Joey Wheeler
@Date=2020/10/22 17:32
*/

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

@Slf4j
//@SpringBootTest
public class OptionalStyleObservable {



    @Test
    public void test(){
        //だめみたいすね
        Observable<String> exist = Observables.ofNullable("val");
        Observable<Object> empty = Observables.ofNullable(null);
        // Optional#ifPresent
        exist.subscribe(System.out::println); // print val
        empty.subscribe(System.out::println); // 表示処理自体も呼ばれない

        /*return Disposable*/
        log.info(Observables.get(exist));
        //log.info(""+Observables.get(empty));//throw java.util.NoSuchElementException

        log.info( Observables.orElse(exist, "default"));//val
        log.info(""+ Observables.orElse(empty, "default"));//default

        log.info(""+Observables.isPresent(exist));//true
        log.info(""+Observables.isPresent(empty));//false


    }

    @Test
    public void test1(){
        Observable.range(1, 5)
                .buffer(2)
                .subscribe(System.out::println);
    }


    /*
    16:06:37.225 [RxCachedThreadScheduler-1] INFO com.dyh.quickdemo.demos.rxjava.OptionalStyleObservable - RxCachedThreadScheduler-1
16:06:38.229 [RxCachedThreadScheduler-1] INFO com.dyh.quickdemo.demos.rxjava.OptionalStyleObservable - RxCachedThreadScheduler-1
16:06:39.229 [RxCachedThreadScheduler-1] INFO com.dyh.quickdemo.demos.rxjava.OptionalStyleObservable - RxCachedThreadScheduler-1
16:06:40.229 [RxCachedThreadScheduler-1] INFO com.dyh.quickdemo.demos.rxjava.OptionalStyleObservable - RxCachedThreadScheduler-1
16:06:41.230 [RxCachedThreadScheduler-1] INFO com.dyh.quickdemo.demos.rxjava.OptionalStyleObservable - RxCachedThreadScheduler-1

        IO 就一个线程
    */
    @Test
    public void test2() throws InterruptedException {
        Observable.range(1, 5)
                .subscribeOn(Schedulers.io())
                .subscribe(i ->{
                    Thread.sleep(1000);
                    log.info(Thread.currentThread().getName());
                });

        Thread.sleep(114514);
    }


    /*
    Observable.range(1, 5)
        .buffer(2)
        .subscribe(System.out::print);
    */


}
